package com.fjec.user.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.fjec.user.entity.SysRole;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * 003.角色表 Mapper 接口
 * </p>
 *
 * @author admin
 * @since 2020-08-31
 */
public interface SysRoleMapper extends BaseMapper<SysRole> {

    @Select("select a.RoleCode from sys_role a, ref_user_role b, sys_user_app c where a.AppType = 1 and a.Id = b.RoleId and b.UserId = c.UserId and c.UserAppId = #{openId}")
    List<String> getRoleCodesByOpenId(@Param("openId") String openId);

    @Select("SELECT RoleCode FROM sys_role r LEFT JOIN ref_user_role ur ON r.Id = ur.RoleId LEFT JOIN sys_user u ON ur.UserId = u.Id WHERE u.UserName = #{userName}))")
    List<String> getRoleCodesByUserName(@Param("userName") String userName);

    @Insert("insert into ref_user_role values (#{userId}, (select id from sys_role where RoleCode = #{roleCode}))")
    int saveUserRole(@Param("userId") Long userId, @Param("roleCode") String roleCode);

    /**
     * 根据用户Id查询用户角色列表
     *
     * @param userId
     * @return
     */
    List<String> getRoleCodesByUserId(@Param("userId") Long userId);
}
